<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px">
      <el-breadcrumb-item :to="{ path: '/' }">后台管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单回收站</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>

    <el-table
        :data="tableData"
        border
        @row-dblclick="getOrderDetails"
        style="width: 100%">
      <el-table-column prop="id" label="ID" align="center"></el-table-column>
      <el-table-column prop="sn" label="订单号" width="170px" align="center"></el-table-column>
      <el-table-column prop="name" label="收件人姓名" align="center"></el-table-column>
      <el-table-column prop="tel" label="收件人手机号" align="center"></el-table-column>
      <el-table-column prop="company" label="快递公司" align="center"></el-table-column>
      <el-table-column prop="message" label="取件信息" align="center"></el-table-column>
      <el-table-column prop="address" label="配送地址" width="200px" align="center"></el-table-column>
      <el-table-column prop="status" label="订单状态" align="center">
        <template slot-scope="scope">
          <div>
            {{getStatus(scope.row)}}
          </div>
        </template>
      </el-table-column>

      <el-table-column label="操作" width="120" align="center">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-refresh-left" circle size="mini"
                     @click.stop="openRestoreConfirm(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>

  </div>
</template>

<script>
export default {
  name: "OrderRecycleBinView",
  data() {
    return {
      tableData: [],
    }
  },
  methods: {
    getStatus(order){
      if(order.status==1){
        return "等待接单"
      }else if(order.status==2){
        return "派送中"
      }else if(order.status==3){
        return "订单完成"
      }else {
        return "订单异常"
      }
    },
    loadOrderList() {
      console.log('在localStorage中的JWT数据：' + localStorage.getItem('jwt'));
      let url = 'http://localhost:9080/orders/delete';
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        console.log(response.data.data);
        this.tableData = response.data.data;
      })
    },
    openRestoreConfirm(order) {
      this.$confirm('此操作将还原【' + order.sn + '】订单，是否继续？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleRestore(order);
      }).catch(() => {
      });
    },
    handleRestore(order) {
      let url = 'http://localhost:9080/orders/' + order.id + '/restore';
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url).then((response) => {
        if (response.data.state != 20000) {
          this.$message.error(response.data.message);
        }
        if (response.data.state == 20000 || response.data.state == 40400) {
          this.loadOrderList();
        }
      })
    },
    getOrderDetails(row){
      console.log(row.id);
      localStorage.setItem('orderId', row.id);
      this.$router.push('/sys-admin/order/recycle-bin/details');
    }
  },
  mounted() {
    this.loadOrderList();
  }
}
</script>

<style scoped>

</style>
